<!--
 * @Description:
 * @Version: 1.0
 * @Author: liguansheng
 * @Date: 2022-08-23 13:41:53
 * @LastEditors: liguansheng
 * @LastEditTime: 2022-08-30 14:00:31
-->
<template>
  <div>
    <el-form
      inline
      label-position="top"
      class="propertyForm"
      :model="form"
      :rules="rules"
      ref="formElRef"
    >
      <el-form-item
        label="Key"
        prop="key"
      >
        <el-input v-model="form.key" />
      </el-form-item>
      <el-form-item
        label="值"
        prop="value"
      >
        <el-input v-model="form.value" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="success"
          :icon="Plus"
          @click="handleAdd"
        >
          添加一个新的
        </el-button>
      </el-form-item>
    </el-form>
    <ul
      class="properties"
      v-if="list.length"
    >
      <li class="title">
        <span class="key">Key</span>
        <span class="value">值</span>
      </li>
      <li
        class="property"
        v-for="(item, index) in list"
        :key="index"
      >
        <span class="key">{{ item.key }}</span>
        <span class="value">{{ item.value }}</span>
        <el-button
          :icon="Delete"
          type="danger"
          size="small"
          @click="handleDelete(index)"
        />
      </li>
    </ul>
  </div>
</template>

<script setup lang="ts">
import { PropType, ref, watch } from 'vue'
import { Plus, Delete } from '@element-plus/icons-vue'
import { IElForm, IElFormRule } from '@/types/element-plus'
import { IProperty } from './types'

const props = defineProps({
  properties: {
    type: Array as PropType<IProperty[]>,
    required: true
  }
})
interface EmitsType {
  (e: 'update:properties', properties: IProperty[]):void
}
const emit = defineEmits<EmitsType>()

const formElRef = ref<IElForm|null>(null)

const form = ref<IProperty>({
  key: '',
  value: ''
})
const rules = ref<IElFormRule>({
  key: [{ required: true, message: '字段不可为空', trigger: 'blur' }],
  value: [{ required: true, message: '字段不可为空', trigger: 'blur' }]
})
interface IItem{
  key: string
  value: string
}
const list = ref<IItem[]>(props.properties)
watch(list, (newValue) => {
  emit('update:properties', newValue)
}, {
  deep: true
})
watch(() => props.properties, (newValue) => {
  list.value = newValue
}, {
  deep: true
})

const handleAdd = async () => {
  await formElRef.value?.validate((valid, fields) => {
    if (valid) {
      list.value.push({
        key: form.value.key,
        value: form.value.value
      })
      form.value.key = ''
      form.value.value = ''
    }
  })
}

const handleDelete = (index: number) => {
  list.value.splice(index, 1)
}
</script>

<style scoped lang="scss">
@import '@/styles/index.scss';
.propertyForm{
  display: flex;
  justify-content: space-between;
  .el-form-item{
    width: 45%;
    margin-bottom: 14px;
  }
}
.properties{
  span{
    display: inline-block;
    line-height: 1.5;
    font-size: 16px;
  }
  .title{
    font-weight: 600;
  }
  .property{
    height: 40px;
    display: flex;
    align-items: center;
    &:nth-child(2n){
      background-color:  #e9e9eb;
    }
  }
  .key{
    width: 33%;
    padding-left: 10px;
    @include ellipsis(1);
  }
  .value{
    width: 60%;
    @include ellipsis(1);
  }
}
</style>
